import { useState, useEffect } from "react";

export default () => {
  // 租赁入口的new标
  const [isShowTag, setShowTag] = useState(true);
  const currentTime = new Date().getTime().toString();
  const firstEntryTime =
    localStorage.getItem("YpEntryDisplayStartTime") || currentTime;
  const sevenDayUnix = (
    parseInt(firstEntryTime) +
    7 * 24 * 60 * 60 * 1000
  ).toString();

  useEffect(() => {
    // 存储首次进入页面后的时间戳
    localStorage.getItem("YpEntryDisplayStartTime") === null &&
      localStorage.setItem("YpEntryDisplayStartTime", currentTime);

    // 存储7个自然日后的时间戳
    localStorage.setItem("YpEntryDisappearEndTime", sevenDayUnix);

    // 再次进入判断当前时间是否大于首次缓存7天后的时间
    const ypEntryDisappearEndTime =
      localStorage.getItem("YpEntryDisappearEndTime") || null;

    if (ypEntryDisappearEndTime !== null) {
      currentTime > ypEntryDisappearEndTime
        ? setShowTag(false)
        : setShowTag(true);
    } else {
      setShowTag(true);
    }
  }, []);

  return isShowTag;
};
